package crypto.des;

import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

public class TestDes {

  public static void main(String[] args) throws Exception {
    // 待加密的字符串对象
    String input = "SealedObject";
// 实例化KeyGenerator对象，使用DES算法
    KeyGenerator kg = KeyGenerator.getInstance("DES");
// 创建秘密密钥
    SecretKey key = kg.generateKey();
    System.out.println("[" + key + "］code:" + key.hashCode());
// 实例化用于加密的Cipher对象cipher1
    Cipher cipher1 = Cipher.getInstance(key.getAlgorithm());
    System.out.println(key.getAlgorithm());
// 初始化
    cipher1.init(Cipher.ENCRYPT_MODE, key);
// 构建SealedObject对象
    SealedObject sealedObject = new SealedObject(input, cipher1);
    System.out.println(sealedObject.getObject(key));
// 实例化用于解密的Cipher对象cipher2
    Cipher cipher2 = Cipher.getInstance(key.getAlgorithm());
// 初始化
    cipher2.init(Cipher.DECRYPT_MODE, key);
// 获得解密后的字符串对象
    String output = (String) sealedObject.getObject(cipher2);

    Map<String, String> m = new HashMap<String, String>();
    m.put("1", "dddd");
    System.out.println(m);

  }
}
